package com.zhuss.team.processor;

import com.zhuss.bean.bo.team.invest.DetailInvestBo;
import com.zhuss.bean.bo.team.publish.CreatePublishBo;
import com.zhuss.bean.bo.team.publish.CreatePublishUserBo;
import com.zhuss.bean.vo.team.invest.DetailInvestVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.core.processor.UserRead;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.*;
import com.zhuss.exception.WpsException;
import com.zhuss.team.db.dao.ext.InvestPublishDao;
import com.zhuss.team.db.entity.InvestPublish;
import com.zhuss.util.JsonUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: zhushanshuo
 * @Date: 2020/3/26 13:25
 */
@Component("publishCreate")
public class PublishCreate extends AbstractProcessor<Object, CreatePublishBo> implements Processor<Object, CreatePublishBo> {

    @Autowired
    private InvestPublishDao investPublishDao;

    @Autowired
    private UserRead userRead;

    @Autowired
    private PublishUserCreate publishUserCreate;

    @Autowired
    private InvestRead investRead;

    @Override
    public Object process(CreatePublishBo bo) throws WpsException {
        String investName = ((DetailInvestVo) investRead.detail(bo.getInvestId())).getInvestName();

        InvestPublish ip = new InvestPublish();
        ip.setInvestId(bo.getInvestId());
        ip.setInvestName(investName);
        ip.setStartTime(bo.getStartTime());
        ip.setEndTime(bo.getEndTime());
        ip.setCompanyId(bo.getCompanyId());
        ip.setCompanyName(bo.getCompanyName());
        ip.setPerson(bo.getPerson());
        ip.setStatus(PublishStatus.已发布.key());
        ip.setSort(bo.getSort());
        ip.setArea(bo.getArea());

        ip.setCreator(bo.getConcurrentUserName());
        ip.setCreatorId(bo.getConcurrentUserId());
        ip.setCreateTime(new Date());
        investPublishDao.create(ip);

        CreatePublishUserBo userBo = new CreatePublishUserBo();
        BeanUtils.copyProperties(bo,userBo);
        List<Long> longList = new ArrayList<>();

        if (bo.getSendType() == SendType.全部.key()){
            Object obj = userRead.listUserId(ip.getArea());
            for (Object o : (List<Long>) obj) {
                longList.add(Long.class.cast(o));
            }
            userBo.setUserIdList(longList);
        }else if (bo.getSendType() == SendType.人员.key()){
            String[] strs = ip.getPerson().split(",");
            for (String s : strs) {
                longList.add(Long.parseLong(s));
            }
            userBo.setUserIdList(longList);
        }else if (bo.getSendType() == SendType.单位.key()){
            List<Long> companyIdList = new ArrayList<>();
            String[] strs = ip.getCompanyId().split(",");
            for (String s : strs) {
                companyIdList.add(Long.parseLong(s));
            }
            Object obj = userRead.listUserIdByCompany(companyIdList);
            for (Object o : (List<Long>) obj) {
                longList.add(Long.class.cast(o));
            }
            userBo.setUserIdList(longList);
        }
        userBo.setPublishId(ip.getPublishId());
        userBo.setStartTime(ip.getStartTime());
        userBo.setEndTime(ip.getEndTime());
        userBo.setInvestId(ip.getInvestId());
        userBo.setStatus(PublishUserStatus.待填写调查.key());
        userBo.setArea(ip.getArea());
        publishUserCreate.process(userBo);
        return null;
    }

    @Override
    public Object log(CreatePublishBo param) throws WpsException {
        OpLog record = new OpLog();
        record.setMoudle(MoudleEnum.协同发布.key());
        record.setAction(LogActionEnum.问卷发布到个人.key());
        record.setTargetId(param.getTargetId());
        record.setTargetName(param.getInvestName());
        record.setContent("参数: " + JsonUtil.toJson(param));
        record.setReason(param.getExceptionReason());

        record.setCreatorId(param.getConcurrentUserId());
        record.setCreator(param.getConcurrentUserName());
        record.setCreateTime(new Date());
        log(record);
        return null;
    }

    @Override
    public void check(CreatePublishBo createPublishBo) throws WpsException {

    }
}
